<?php


namespace App\Http\Backend\Controllers;


use App\Http\Models\Users;
use App\Http\Utils\Api;
use Illuminate\Http\Request;

class UserController extends Controller
{


    /**
     * 获取用户信息
     * @param Request $request
     * @return array
     */
    public function getUser(Request $request){
            $id=$request->get('id');
            $user=Users::query()->select(['id','username','status','avatar','mobile','email'])->find($id);
           return success($user);
    }


    public function add(Request $request)
    {
        $data=$request->post();
        $data['salt']=getRandomStr(50);
        $Users = new Users();
        $validator = $Users->Validator($data);
        if ($validator->fails()) {
            return Api::Fail($validator->errors()->first());
        }
        $data['password']=password_hash($data['password'],PASSWORD_DEFAULT);
        if ($Users->fill($data)->save()) {
            return Api::Success();
        }
    }

    public function update(){
        $data = [
            'username' => "asd",
            'mobile' => "woddp",
            'salt' => "woddp",
            'token' => "woddp",
            'avatar' => "woddp",
            'password' => "woddp",
        ];
        $id=20;
        $Users = new Users();

        $validator =$Users->Validator($data);
        if ($validator->fails()) {
            return Api::Fail($validator->errors()->first());
        }
        dd($Users->save());
//       $user= Users::find($id);
//        $user->nickname="sdacsdc";
//       dd($user->save());
    }


}
